package com.meichis.yndms.adapter.RecycleViewItemType;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meichis.mcsappframework.adapter.recyclerview.base.ItemViewDelegate;
import com.meichis.mcsappframework.adapter.recyclerview.base.ViewHolder;
import com.meichis.mcsappframework.utils.DateUtil;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.mcsappframework.utils.ScreenUtil;
import com.meichis.yndms.R;
import com.meichis.yndms.component.ChoicePopView;
import com.meichis.yndms.model.InspectTemplate_Item;
import com.meichis.yndms.ui.activity.VST_FreeQuestionnaireLogicActivity;

import java.util.Calendar;
import java.util.List;

/**
 * Created by pei on 2016/8/24.
 * 默认item
 */
public class FillMode_TextView implements ItemViewDelegate<InspectTemplate_Item> {
    private final Activity activity;
    private List<InspectTemplate_Item> items;
    private Calendar mCalendar;

    public FillMode_TextView(Activity activity, List<InspectTemplate_Item> items) {
        this.items = items;
        this.activity = activity;
        mCalendar = Calendar.getInstance();
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.fill_mode_text;
    }

    @Override
    public boolean isForViewType(InspectTemplate_Item item, int position) {
        return item.getItem().getFillMode() == 6 || item.getItem().getFillMode() == 7;
    }

    @Override
    public void convert(final ViewHolder holder, final InspectTemplate_Item item, int position) {
        TextView tv_group = holder.getView(R.id.tv_group);
        if (position == 0) {
            if (!TextUtils.isEmpty(item.getItemGroup())) {
                tv_group.setVisibility(View.VISIBLE);
                holder.setVisible(R.id.v_group, true);
                tv_group.setText(item.getItemGroup());
            } else {
                tv_group.setVisibility(View.GONE);
                holder.setVisible(R.id.v_group, false);
            }
        } else {
            if (!TextUtils.isEmpty(item.getItemGroup()) && !item.getItemGroup().equals(items.get(position - 1).getItemGroup())) {
                tv_group.setVisibility(View.VISIBLE);
                holder.setVisible(R.id.v_group, true);
                tv_group.setText(item.getItemGroup());
            } else {
                tv_group.setVisibility(View.GONE);
                holder.setVisible(R.id.v_group, false);
            }
        }
        holder.setText(R.id.tv_question, Html.fromHtml(String.valueOf(position + 1) + ". " +
                item.getItem().getName() +
                (!TextUtils.isEmpty(item.getItem().getUnit()) ? "(" + item.getItem().getUnit() + ")" : "") +
                (item.getIsMustFill() == 1 ? " <font  color=\"#FF0000\">*</font>" : "") +
                (!TextUtils.isEmpty(item.getItem().getSubTitle()) ? "<br/> <font color=\"#8D8D8D\" ><small>" + item.getItem().getSubTitle() + "</small></font>" : "")
        ));
        holder.getView(R.id.iv_data).setVisibility(item.getItem().getFillMode() == 7 ? View.VISIBLE : View.GONE);
        final LinearLayout ll = holder.getView(R.id.ll_answer);
        setBackground(item, ll);
        final TextView tv_answer = holder.getView(R.id.tv_answer);
        tv_answer.setText(item.getFillName());
        tv_answer.setMinHeight(ScreenUtil.dip2px(item.getItem().getFillMode() == 2 ? 80 : 40));
        holder.setOnClickListener(R.id.iv_clear, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_answer.setText("");
                item.setFillName("");
                item.setFillValue("");
            }
        });
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (item.getItem().getFillMode()) {
                    case 6:
                        new ChoicePopView<>(holder.getConvertView().getContext(), item.getItem().getOptions(), "Name", new ChoicePopView.SelectedListener() {
                            @Override
                            public void choice(int position) {
                                tv_answer.setText(item.getItem().getOptions().get(position).getName());
                                item.setFillName(item.getItem().getOptions().get(position).getName());
                                item.setFillValue(item.getItem().getOptions().get(position).getName());
                                setBackground(item, ll);
                            }
                        }).showAtLocation(tv_answer, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 1);
                        return;
                    case 7:
                        new DatePickerDialog(holder.getConvertView().getContext(), AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                mCalendar.set(year, monthOfYear, dayOfMonth);
                                String data = DateUtil.date2Str(mCalendar, DateUtil.FORMAT_YMD);
                                if (!TextUtils.isEmpty(item.getItem().getMinValue()) && !DateUtil.CompareDate(DateUtil.DateTime2Str(item.getItem().getMinValue(), DateUtil.FORMAT_YMD), data, DateUtil.FORMAT_YMD)) {
                                    MessageTools.showLongToast("最小日期为：" + DateUtil.DateTime2Str(item.getItem().getMinValue(), DateUtil.FORMAT_YMD));
                                    return;
                                }
                                if (!TextUtils.isEmpty(item.getItem().getMaxValue()) && !DateUtil.CompareDate(data, DateUtil.DateTime2Str(item.getItem().getMaxValue(), DateUtil.FORMAT_YMD), DateUtil.FORMAT_YMD)) {
                                    MessageTools.showLongToast("最大日期为：" + DateUtil.DateTime2Str(item.getItem().getMaxValue(), DateUtil.FORMAT_YMD));
                                    return;
                                }
                                tv_answer.setText(data);
                                item.setFillName(data);
                                item.setFillValue(data);
                                setBackground(item, ll);
                            }
                        }, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH)).show();
                        return;
                    default:
                        break;
                }
            }
        });
    }

    private void setBackground(InspectTemplate_Item item, LinearLayout ll) {
        ll.setBackgroundResource(item.getFillName().length() == 0 && item.getIsMustFill() == 1 ? R.drawable.bg_ll_frame : R.drawable.bg_ll_frame);
    }
}
